﻿#include "wpns_a2_w.sqf"
#include "wpns_a2_e.sqf"
#include "wpns_oa_w.sqf"
#include "wpns_oa_e.sqf"
#include "wpns_baf_w.sqf"
#include "wpns_pmc.sqf"
#include "wpns_excl.sqf"

//#define __DEBUG__

if (isNil "WEST_AMMO_BOX_CONTENT") then { WEST_AMMO_BOX_CONTENT = []; };
if (isNil "EAST_AMMO_BOX_CONTENT") then { EAST_AMMO_BOX_CONTENT = []; };

if ((count WEST_AMMO_BOX_CONTENT > 0) && (count EAST_AMMO_BOX_CONTENT > 0)) exitWith { diag_log ["WEAPON ARRAY BUILDER: Both arrays are overloaded",(count WEST_AMMO_BOX_CONTENT),(count EAST_AMMO_BOX_CONTENT)]; };

d_weapon_coeff = 2;
_weaponcoeff = d_weapon_coeff;

_weaponcoeff = floor(_weaponcoeff);
if (_weaponcoeff <= 0) then {_weaponcoeff = 1};
// coeff for bigger weapons like launchers and machine guns.
_bigcoeff = floor(_weaponcoeff / 2);
if (_bigcoeff <= 0) then {_bigcoeff = 1};

// Table of all weapons
_alltheweapons = [];
// Table for GPS, etc.
_extraitems = [];

// Contains a sub array per weapon type
_weapons_arrays = [];

// Contains the different possible slots and indicates the order of the subarrays here above
_indexing_array = [];

_allwpns = configFile >> "CfgWeapons";
for "_a" from 1 to ((count _allwpns) - 1) do {
	_base = configFile >> "CfgWeapons";
	_wpn = (_base select _a);
	if (getNumber(_wpn >> "scope") == 2) then {
		_slot = getNumber(_wpn >> "type");
		// excluding "hard mounted" weapons
		if (_slot != 65536) then {
			// This line allows for regrouping all extra items together.
			if (_slot > 4096) then {_slot = 4096};
			_index = _indexing_array find _slot;
			If (_index  < 0) then {
				_index = count _indexing_array;
				_indexing_array set [count _indexing_array, _slot];
				_weapons_arrays set [_index, []];
			};
			_wpn_sub_array = _weapons_arrays select _index;
			
			// Only weapons which have a slot higher than 0 need to be added. Slot 0 is for satchels and hand grenades. They aren't weapons but magazines.
			If (_slot > 0) then {
				// converts the string name (in the form: "bin\config.bin/CfgWeapons/M9") to an array of unicode characters
				_cfgtbl = toArray (format ["%1",_wpn]);
				// stores the new array of characters with only the weapon name ("M9" in our example)
				_nametbl = [];
				// Eliminating the string "BIN\CONFIG.BIN/CFGWEAPONS/". The last / is at pos 26 in the char array
				for "_s" from 26 to ((count _cfgtbl) - 1) do { _nametbl = _nametbl + [_cfgtbl select _s] };
				// Converts the char array back into string
				_name = toString _nametbl;

				_wpn_sub_array = _wpn_sub_array + [_name];
			};
			_weapons_arrays set [_index, _wpn_sub_array];
		};
	};
};

#ifdef __DEBUG__		
diag_log Format ["Indexing: %1",_indexing_array];
for "_i" from 0 to ((count _weapons_arrays) - 1) step 1 do {
	diag_log Format ["Slot %1 : %2",_indexing_array select _i,_weapons_arrays select _i];
};
#endif		

// We build the array of weapons for the box.
_alltheweapons = [];
_allthemagazines = [];
_extraitems = [];
// First: Rifles and the like
_index = _indexing_array find 1;
if (_index > 0) then {
	if (count (_weapons_arrays select _index) > 0) then {
		_alltheweapons = _alltheweapons + (_weapons_arrays select _index);
	};
	// We remove the already added array
	_weapons_arrays set [_index, "XdelX"];
	_weapons_arrays = _weapons_arrays - ["XdelX"];
	_indexing_array = _indexing_array - [1];
};

// Second: Machine guns and the like
_index = _indexing_array find 5;
if (_index > 0) then {
	if (count (_weapons_arrays select _index) > 0) then {
		_alltheweapons = _alltheweapons + (_weapons_arrays select _index);
	};
	// We remove the already added array
	_weapons_arrays set [_index, "XdelX"];
	_weapons_arrays = _weapons_arrays - ["XdelX"];
	_indexing_array = _indexing_array - [5];
};

// Third: Launchers and the like
_index = _indexing_array find 4;
if (_index > 0) then {
	if (count (_weapons_arrays select _index) > 0) then {
		_alltheweapons = _alltheweapons + (_weapons_arrays select _index);
	};
	// We remove the already added array
	_weapons_arrays set [_index, "XdelX"];
	_weapons_arrays = _weapons_arrays - ["XdelX"];
	_indexing_array = _indexing_array - [4];
};
// Fourth: Pistols and the like
_index = _indexing_array find 2;
if (_index > 0) then {
	if (count (_weapons_arrays select _index) > 0) then {
		_alltheweapons = _alltheweapons + (_weapons_arrays select _index);
	};
	// We remove the already added array
	_weapons_arrays set [_index, "XdelX"];
	_weapons_arrays = _weapons_arrays - ["XdelX"];
	_indexing_array = _indexing_array - [2];
};
// Fifth: Binoculars and other extras
_index = _indexing_array find 4096;
if (_index > 0) then {
	if (count (_weapons_arrays select _index) > 0) then {
		_extraitems = _extraitems + (_weapons_arrays select _index);
	};
	// We remove the already added array
	_weapons_arrays set [_index, "XdelX"];
	_weapons_arrays = _weapons_arrays - ["XdelX"];
	_indexing_array = _indexing_array - [4096];
};
// Sixth, we add any remaining array (if any) to the weapon array in the order they were added.
{
	if (count _x > 0) then {
		_alltheweapons = _alltheweapons + _x;
	};
} foreach _weapons_arrays;

// Lastly, we remove the excludes
_alltheweapons = _alltheweapons - _wpns_exclude;
_extraitems = _extraitems - _wpns_exclude;

#ifdef __DEBUG__		
diag_log Format ["Weapons: %1",_alltheweapons];
diag_log Format ["Extras: %1",_extraitems];
#endif		

// Now we need to create the arrays for both player and enemy side

_allwestweapons = +_alltheweapons;
_alleastweapons = +_alltheweapons;

_allwestweapons = _allwestweapons - _wpns_a2_e;
_allwestweapons = _allwestweapons - _wpns_oa_e;
// East receive all weapons that the player do not have.
_alleastweapons = _alleastweapons - _allwestweapons;

// Both sides receive the same grenades and satchels
_allwestweapons = _allwestweapons + ["put","throw"];
_alleastweapons = _alleastweapons  + ["put","throw"];
#ifdef __DEBUG__		
diag_log Format ["West Weapons: %1",_allwestweapons];
diag_log Format ["East Weapons: %1",_alleastweapons];
#endif		

// Now we create the content of the player ammobox
_west_ammo_box = [];
_box_weapons = [];
_box_magazines = [];
// This allows for tracking what has already been added and to prevent adding unwanted things
_added_magazines = _mags_exclude;
for "_a" from 0 to ((count _allwestweapons) - 1) do {
	_wpn = (_allwestweapons select _a);
	_slot = getNumber (configFile >> "cfgWeapons" >> _wpn >> "type");
	// excluding "hard mounted" weapons and extra items, they should already be filtered... but yet.
	if (_slot < 4096) then {
		
		// Only weapons which have a slot higher than 0 need to be added. Slot 0 is for satchels and hand grenades. They aren't weapons but magazines.
		_coeff = _weaponcoeff;
		// Slot 0 is for handgrenades/satchels which are not weapons but magazines
		If (_slot > 0) then {
			If (_slot > 2) then { _coeff = _bigcoeff; };
			_box_weapons = _box_weapons + [[1,_wpn,_coeff]];
		};
		// We want a minimum of 20 of each ammunition type.
		if (_coeff < 2) then {_coeff = 2};
		// Adding default ammunitions
		_wpncfg = configFile >> "CfgWeapons" >> _wpn;
		_cfgmags = getArray (_wpncfg >> "magazines");
		if (count _cfgmags > 0) then {
			for "_k" from 0 to ((count _cfgmags) - 1) do {
				_mag = _cfgmags select _k;
				if (not (_mag in _added_magazines)) then {
					_added_magazines = _added_magazines + [_mag];
					_box_magazines = _box_magazines + [[0, _mag, _coeff * 15]];
				};
			};
		};

		// Adding secondary ammunitions (for grenade launchers for example)
		_wpnmuzzles = getArray (_wpncfg >> "muzzles");
		// _wpnmuzzles > 1 because the first muzzle is always the primary weapon ("This")
		if (count _wpnmuzzles > 1) then {
			for "_m" from 1 to ((count _wpnmuzzles) - 1) do {
				// Muzzles are a sub-weapon type, thus they have magazines of their own
				_muzzlmags = getArray(configFile >> "cfgWeapons" >> _wpn >> (_wpnmuzzles select _m) >> "magazines");
				for "_p" from 0 to ((count _muzzlmags) - 1) do {
					_mag = _muzzlmags select _p;
					if (not (_mag in _added_magazines)) then {
						_added_magazines = _added_magazines + [_mag];
						_box_magazines = _box_magazines + [[0, _mag, _coeff * 15]];
					};
				};
			};
		};
	};
};
_west_ammo_box = _box_weapons + _box_magazines;


// Now we create the content of the enemy ammobox
_east_ammo_box = [];
_box_weapons = [];
_box_magazines = [];
_added_magazines = _mags_exclude;
for "_a" from 0 to ((count _alleastweapons) - 1) do {
	_wpn = (_alleastweapons select _a);
	_slot = getNumber (configFile >> "cfgWeapons" >> _wpn >> "type");
	// excluding "hard mounted" weapons and extra items, they should already be filtered... but yet.
	// We also check the excluded weapons, just in case :)
	if ((_slot < 4096) AND (!(_wpn in _wpns_exclude))) then {
		
		// Only weapons which have a slot higher than 0 need to be added. Slot 0 is for satchels and hand grenades. They aren't weapons but magazines.
		_coeff = _weaponcoeff;
		// Slot 0 is for handgrenades/satchels which are not weapons but magazines
		If (_slot > 0) then {
			If (_slot > 2) then { _coeff = _bigcoeff; };
			_box_weapons = _box_weapons + [[1,_wpn,_coeff]];
		};
		// We want a minimum of 20 of each ammunition type.
		if (_coeff < 2) then {_coeff = 2};
		// Adding default ammunitions
		_wpncfg = configFile >> "CfgWeapons" >> _wpn;
		_cfgmags = getArray (_wpncfg >> "magazines");
		if (count _cfgmags > 0) then {
			for "_k" from 0 to ((count _cfgmags) - 1) do {
				_mag = _cfgmags select _k;
				if (not (_mag in _added_magazines)) then {
					_added_magazines = _added_magazines + [_mag];
					_box_magazines = _box_magazines + [[0, _mag, _coeff * 15]];
				};
			};
		};

		// Adding secondary ammunitions (for grenade launchers for example)
		_wpnmuzzles = getArray (_wpncfg >> "muzzles");
		// _wpnmuzzles > 1 because the first muzzle is always the primary weapon ("This")
		if (count _wpnmuzzles > 1) then {
			for "_m" from 1 to ((count _wpnmuzzles) - 1) do {
				// Muzzles are a sub-weapon type, thus they have magazines of their own
				_muzzlmags = getArray(configFile >> "cfgWeapons" >> _wpn >> (_wpnmuzzles select _m) >> "magazines");
				for "_p" from 0 to ((count _muzzlmags) - 1) do {
					_mag = _muzzlmags select _p;
					if (not (_mag in _added_magazines)) then {
						_added_magazines = _added_magazines + [_mag];
						_box_magazines = _box_magazines + [[0, _mag, _coeff * 15]];
					};
				};
			};
		};
	};
};
_east_ammo_box = _box_weapons + _box_magazines;

// And finally we add the extra items to both boxes
_box_magazines = [];
_added_magazines = _mags_exclude;
{
	_west_ammo_box = _west_ammo_box + [[1,_x,_weaponcoeff]];
	_east_ammo_box = _east_ammo_box + [[1,_x,_weaponcoeff]];
	// Adding default ammunitions if any
	// Batteries for the laser designator for example 
	_wpncfg = configFile >> "CfgWeapons" >> _x;
	_cfgmags = getArray (_wpncfg >> "magazines");
	if (count _cfgmags > 0) then {
		for "_k" from 0 to ((count _cfgmags) - 1) do {
			_mag = _cfgmags select _k;
			if (not (_mag in _added_magazines)) then {
				_added_magazines = _added_magazines + [_mag];
				_box_magazines = _box_magazines + [[0, _mag, _weaponcoeff * 15]];
			};
		};
	};
}foreach _extraitems;
_west_ammo_box = _west_ammo_box + _box_magazines;
_east_ammo_box = _east_ammo_box + _box_magazines;

if (count WEST_AMMO_BOX_CONTENT <= 0) then { WEST_AMMO_BOX_CONTENT = _west_ammo_box; };
if (count EAST_AMMO_BOX_CONTENT <= 0) then { EAST_AMMO_BOX_CONTENT = _east_ammo_box; };

// [_west_ammo_box,""] call XfArrayDump;

true;
